<?php


namespace app\crm\model;

use app\admin\model\Common;


class CustomerAddress extends Common
{

    protected $name = 'crm_customer_address';

    /**
     * 新增地址
     * @param  array $request 请求参数
     * @return bool
     * @author Mr.Liu Wuchen
     * @create 2021-06-04 11:25
     */
    public function addData(array $request): bool
    {
        try {
            // 验证
            $validate = new \app\crm\validate\CustomerAddress();
            if (!$validate->scene('create')->check($request)) {
                $this->error = $validate->getError();
                return false;
            }
            $data = [
                'customer_id' => $request['customer_id'],
                'consignee'   => trim($request['consignee']),
                'mobile'      => trim($request['mobile']),
                'province'    => $request['province'],
                'city'        => $request['city'],
                'district'    => $request['district'],
                'address'     => trim($request['address']),
                'create_time' => time(),
                'update_time' => time(),
            ];

            $unitId = $this->insertGetId($data);
            if ($unitId) {
                $this->error = '添加成功';
                return true;
            }
        } catch (\Exception $e) {
        }
        $this->error = '添加失败';
		return false;
    }

    /**
     * 更新地址
     * @param array $request 请求参数
     * @return bool
     * @author Mr.Liu Wuchen
     * @create 2021-06-04 11:33
     */
    public function saveData(array $request): bool
    {
        try {
            $addressId = $request['address_id'] ?? 0;
            if (!$addressId) {
                $this->error = '参数错误';
                return false;
            }
            // 验证
            $validate = new \app\crm\validate\CustomerAddress();
            if (!$validate->scene('update')->check($request)) {
                $this->error = $validate->getError();
                return false;
            }
            $data = [
                'consignee'   => trim($request['consignee']),
                'mobile'      => trim($request['mobile']),
                'province'    => $request['province'],
                'city'        => $request['city'],
                'district'    => $request['district'],
                'address'     => trim($request['address']),
                'update_time' => time(),
            ];

            $bool = $this->where(['address_id'=>$addressId])->update($data);
            if ($bool) {
                $this->error = '更新成功';
                return true;
            }
        } catch (\Exception $e) {
        }
        $this->error = '更新失败';
		return false;
    }

}